<template>
  <div>
    <el-card shadow="always">
      <el-form ref="management" :inline="true" size="small" style="display:flex;justify-content: space-between">
        <el-form-item>
          <el-form-item >
            <el-input placeholder="请输入服务名" v-model="queryParams.sname" style="width: 300px;">
              <el-button slot="append" icon="el-icon-search" @click="getlist"></el-button>
              <el-button slot="append" type="primary" icon="el-icon-refresh" round
                         @click="selectlist"></el-button>
            </el-input>


          </el-form-item>
        </el-form-item>

      </el-form>
      <el-table  stripe :data="tableData1">
        <el-table-column prop="id" label="id" ></el-table-column>
        <el-table-column prop="sname"  label="能力名称"></el-table-column>
        <el-table-column prop="levelgroup" label="默认节流策略">
          <template slot-scope="scope">
                        <span v-for="(i, index) in scope.row.levelgroup"
                              v-if="index < scope.row.levelgroup.length - 1">{{ i }}&nbsp;/</span>
            <span v-else>{{ i }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="210%">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-row>
        <el-col style="text-align:right">

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getlist"
          />
        </el-col>
      </el-row>
      <!-- 新增对话框 -->
      <!-- <el-dialog title="新增高级策略" :visible.sync="openAdd" center>
          <el-form :model="form" ref="form" :rules="rules" label-position="left">
              <el-form-item label="能力id" prop="orgId" :label-width="formLabelWidth">
                  <el-input v-model="form.orgId"></el-input>
              </el-form-item>
              <el-form-item label="能力名称" prop="orgName" :label-width="formLabelWidth">
                  <el-select v-model="form.orgName" placeholder="请选择机构名称">
                      <el-option label="区域一" value="shanghai"></el-option>
                      <el-option label="区域二" value="beijing"></el-option>
                  </el-select>
              </el-form-item>
              <el-form-item label="默认节流策略" prop="orgCode" :label-width="formLabelWidth">
                  <el-input v-model="form.orgCode"></el-input>
              </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer" :label-width="formLabelWidth">
              <el-button @click="cancel">取 消</el-button>
              <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
      </el-dialog> -->

      <!-- 修改对话框 -->
      <el-dialog title="修改机构" :visible.sync="openEdit" center>
        <el-form :rules="rules2" :model="form" ref="form" label-position="left">
          <el-form-item  disabled label="能力id" prop="sname" :label-width="formLabelWidth">
            <el-input disabled v-model="form.sname"></el-input>
          </el-form-item>
          <el-form-item label="默认节流策略" prop="levelgroup" :label-width="formLabelWidth">
            <el-checkbox-group v-model="form.levelgroup">
              <el-checkbox label="1000"></el-checkbox><br>
              <el-checkbox label="2000"></el-checkbox><br>
              <el-checkbox label="3000"></el-checkbox><br>
              <el-checkbox label="4000"></el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer" :label-width="formLabelWidth">
          <el-button @click="openEdit = false">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>

import { getserlistFn, selectlistFn, updatelistFn } from '@/api/serlist'

export default {
  name: 'orgManagement',
  data() {
    return {
      checklist: [],
      tableData1: [],
      keyWord: '',
      search: '',
      total: 1,
      sid: '',
      queryParams: {//分页参数
        pageNum: 1,
        pageSize: 10,
        sname:null
      },
      openAdd: false,//新增对话框,
      openEdit: false,//修改对话框
      openDetail: false,//详情对话框
      form: {},
      formLabelWidth: '120px',
      rules2: {
        levelgroup: [
          { validator: this.validateLevelGroup, trigger: 'change' }
        ]
      },
      rules: {
        orgId: [
          { required: true, message: "能力id不能为空", trigger: "blur" }
        ],
        orgName: [
          { required: true, message: "能力名称不能为空", trigger: "blur" }
        ],
        orgCode: [
          { required: true, message: "默认节流策略不能为空", trigger: "blur" }
        ],
      }
    }
  },
  methods: {
    validateLevelGroup(rule, value, callback) {
      if (value.length === 0) {
        callback(new Error('请选择至少一个选项'));
      } else {
        callback();
      }
    },
    getlist() {
      this.loading = true
      getserlistFn(this.queryParams).then(res => {
        this.tableData1 = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    cancel() {
      this.reset()
      this.openAdd = false
      this.openEdit = false
    },
    handleAdd() {
      this.$refs['form']?.clearValidate()
      this.reset()
      this.openAdd = true
    },
    selectlist() {
      this.loading = true
      selectlistFn().then(res => {
        this.queryParams.sname=null
        this.getlist()

      })

    },
    handleEdit(index, row) {
      this.form = { ...row }
      this.openEdit = true
    },
    detail(index, row) {
      this.form = { ...row }
      this.openDetail = true
    },
    handleDelete(index, row) {
      this.tableData1.splice(index, 1)
    },
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {

          updatelistFn(this.form).then(res => {
            this.$modal.msgSuccess('修改成功');
            this.getlist()
            this.openEdit = false
          }).catch((error) => {
            console.error(error);
          });

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleSizeChange(val) {
      this. queryParams.currentPage = 1
      this. queryParams.pageSize = val
    },
    handleCurrentChange(val) {
      this. queryParams.currentPage = val
    }
  },
  created() {
    this.getlist()
  }
}

</script>
<style scoped lang="scss">
/* 卡片样式 */
.el-card {
  width: 98%;
  margin: 10px auto;
}

.header {
  height: 80px;
  line-height: 80px;
  position: relative;
}


.header .el-button {
  margin-left: 89%;
}


/* 分页器样式 */
.block {
  margin: 20px 0 0 60%;
}
</style>
